package com.example.writelogdemo.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 时间工具类
 */
public class TimeUtils {
    /**
     * 格式化视频录制时间 ，返回 时：分：秒 格式
     */
    public static String formatRecordVideoTime(int timeValue) {
        String time = "00:00:00";
        if (timeValue == 0) {
            return time;
        }
        int hour = 0;
        int minute = 0;
        int second;
        if (timeValue >= 3600) {
            hour = timeValue / 3600;
        }
        if (timeValue >= 60) {
            minute = timeValue / 60;
            minute = minute % 60;
        }
        second = timeValue % 60;

        String hourStr = getTimeValue(hour) + ":";
        String minuteStr = getTimeValue(minute) + ":";
        String secondStr = getTimeValue(second);
        return hourStr + minuteStr + secondStr;
    }

    public static String formatVideoTime(int timeValue) {
        String time = "00:00";
        if (timeValue == 0) {
            return time;
        }
        int hour = 0;
        int minute = 0;
        int second;
        if (timeValue >= 3600) {
            hour = timeValue / 3600;
        }
        if (timeValue >= 60) {
            minute = timeValue / 60;
            minute = minute % 60;
        }
        second = timeValue % 60;

        String minuteStr = getTimeValue(minute) + ":";
        String secondStr = getTimeValue(second);
        return minuteStr + secondStr;
    }

    /**
     * 格式化 时间数字
     */
    public static String getTimeValue(int value) {
        String num = "";
        if (value == 0) {
            num = "00";
        } else if (value < 10) {
            num = "0" + value;
        } else {
            num = value + "";
        }
        return num;
    }

    /**
     * 视频显示时间
     *
     * @param time 毫秒
     * @return string
     */
    public static String returnMediaTime(long time) {
        int secondTime = (int) (time / 1000);
        if (secondTime < 10) {
            return "0:0" + secondTime;
        } else if (secondTime < 60) {
            return "0:" + secondTime;
        } else if (secondTime < 3600) {
            int remainder = (int) (secondTime % 60);
            return secondTime / 60 + ":" + (remainder < 10 ? "0" + remainder : remainder);
        } else {
            int remainder = (int) (secondTime % 3600);
            int quotient = remainder / 60;
            int sRemainder = remainder % 60;
            return secondTime / 3600 + ":" + (quotient < 10 ? "0" + quotient : quotient) + ":" + (sRemainder < 10 ?
                    "0" + sRemainder : sRemainder);
        }
    }

    /**
     * 格式化时间
     */
    public static String parserTimeToYmdHm(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
        return format.format(new Date(time));
    }

    /**
     * 格式化时间
     */
    public static String parserTimeToYmdHmSpace(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        return format.format(new Date(time));
    }

    /**
     * 格式化时间
     */
    public static String parserTimeToYmd(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        return format.format(new Date(time));
    }

    /**
     * 格式化时间
     */
    public static String parserTimeToHm(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.US);
        return format.format(new Date(time));
    }

    /**
     * 格式化时间
     */
    public static String parserTimeToDate(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
        return format.format(new Date(time));
    }

    /**
     * 根据毫秒返回时分秒
     *
     * @param time
     * @return
     */
    public static String getFormatHMS(long time) {
        time = time / 1000;//总秒数
        int s = (int) (time % 60);//秒
        int m = (int) (time / 60);//分
        int zm = m % 60;//除去小时的分
        int h = (int) (time / 3600);//秒
        return String.format(Locale.ENGLISH, "%02d:%02d:%02d", h, zm, s);
    }
}
